/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-09-25     wbj       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <rtdbg.h>
#include <SG_90.h>
#include <OLED.h>
#define EVENT_FLAG1 (1 << 1)
#define EVENT_FLAG2 (1 << 2)
#define EVENT_FLAG3 (1 << 3)
#define EVENT_FLAG4 (1 << 4)
#define EVENT_FLAG5 (1 << 5)
static struct rt_event event;
extern struct rt_i2c_bus_device *i2c_bus;

static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
static rt_thread_t tid3 = RT_NULL;
static rt_thread_t tid4 = RT_NULL;
static rt_thread_t tid5 = RT_NULL;
static rt_thread_t tid1_oled = RT_NULL;
//static rt_thread_t tid2_oled = RT_NULL;
//static rt_thread_t tid3_oled = RT_NULL;
rt_device_t u2_dev;
struct rt_semaphore sem;
rt_thread_t u2_th;
struct serial_configure u2_configs = RT_SERIAL_CONFIG_DEFAULT;//这个是与电脑通信的，这个结构体后续要改！！！
char buffer;//串口传进来的值
rt_err_t rx_callback(rt_device_t dev, rt_size_t size)
{
    rt_sem_release(&sem);
    return RT_EOK;
}

void u2_rcv_entry(void *parameter)
{
    while(1)
    {
        while(rt_device_read(u2_dev,0,&buffer,1)!=1)
        {
            rt_sem_take(&sem, RT_WAITING_FOREVER);
        }
        if(buffer==1)     //前进
        {
            rt_event_send(&event, EVENT_FLAG1);
            LOG_D("执行前进");
        }
        else if(buffer==2) //趴下
        {
            rt_event_send(&event, EVENT_FLAG2);
            LOG_D("执行趴下");
        }
        else if (buffer==3) //站好
        {
            rt_event_send(&event, EVENT_FLAG3);
            LOG_D("执行站好");
        }
        else if (buffer==4) //坐下
        {
            rt_kprintf("sit...");
            rt_event_send(&event, EVENT_FLAG4);

        }
        else if (buffer==5) //摇摆
        {
            rt_event_send(&event, EVENT_FLAG5);
            LOG_D("执行摇摆");
        }
    }
}
void th1_entry(void *parameter)
{
    while(1)
    {
        rt_uint32_t e;
        if (rt_event_recv(&event,EVENT_FLAG1,
                        RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
                        RT_WAITING_FOREVER, &e) == RT_EOK)
            while(1)
            {
                qianjin();
                if (buffer!=1) {
                    break;
                }
            }
    }
}
void th2_entry(void *parameter)
{
    while(1)
    {
        rt_uint32_t e;
        if (rt_event_recv(&event,EVENT_FLAG2,
                      RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
                      RT_WAITING_FOREVER, &e) == RT_EOK)
            paxia();
            rt_thread_mdelay(20);
    }
}
void th3_entry(void *parameter)
{
    while(1)
    {
        rt_uint32_t e;
        if (rt_event_recv(&event,EVENT_FLAG3,
                          RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
                          RT_WAITING_FOREVER, &e) == RT_EOK)
            zhanhao();
            rt_thread_mdelay(20);
    }
}
void th4_entry(void *parameter)
{
    while(1)
    {
        rt_uint32_t e;
        if (rt_event_recv(&event,EVENT_FLAG4,
                          RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
                          RT_WAITING_FOREVER, &e) == RT_EOK)
            zuoxia();
            rt_thread_mdelay(20);
    }
}
void th5_entry(void *parameter)
{
    while(1)
    {
        rt_uint32_t e;
        if (rt_event_recv(&event,EVENT_FLAG5,
                          RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
                          RT_WAITING_FOREVER, &e) == RT_EOK)
            while(1)
            {
                yaobai();
                if(buffer!=5){
                    break;
                }
            }

    }
}
void th1_oled_entry(void *parameter)
{
    i2c_bus=(struct rt_i2c_bus_device *)rt_device_find("i2c1");
      if(i2c_bus==RT_NULL)
      {
          rt_kprintf("no device \n");
      }
      else
      {
          rt_kprintf("find device \n");

          //初始化程序段
          OLED_INIT();

      }
      OLED_ShowImage(0,0,101,64,kaixing);
      OLED_Update();
    while(1)
    {

    }
}
void Event(void)
{
    rt_event_init(&event, "event", RT_IPC_FLAG_PRIO);
    tid1= rt_thread_create("th1", th1_entry, NULL, 512, 13, 5); //前进
    rt_thread_startup(tid1);
    tid2= rt_thread_create("th2", th2_entry, NULL, 512, 11, 5); //趴下
    rt_thread_startup(tid2);
    tid3= rt_thread_create("th3", th3_entry, NULL, 512, 11, 5); //站好
    rt_thread_startup(tid3);
    tid4= rt_thread_create("th4", th4_entry, NULL, 512, 11, 5); //坐下
    rt_thread_startup(tid4);
    tid5= rt_thread_create("th5", th5_entry, NULL, 512, 13, 5); //摇摆
    rt_thread_startup(tid5);
    tid1_oled= rt_thread_create("th1_oled", th1_oled_entry, NULL, 275, 14, 5);
    rt_thread_startup(tid1_oled);
//    tid2_oled= rt_thread_create("th2_oled", th2_oled_entry, NULL, 512, 11, 5); //坐下
//    rt_thread_startup(tid2_oled);
//    tid3_oled= rt_thread_create("th3_oled", th3_oled_entry, NULL, 512, 12, 5); //摇摆
//    rt_thread_startup(tid3_oled);
}
int Uart(void)
{
    rt_err_t ret=0;
    u2_dev=rt_device_find("uart2");
    if(u2_dev== RT_NULL)
    {
        LOG_E("rt_device_find[uart2] failed ...\n");
        return -EINVAL;
    }

    ret=rt_device_open(u2_dev, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_INT_RX);
    if(ret<0)
    {
        LOG_E("rt_device_open[uart2] failed ...\n");
        return ret;
    }

    rt_device_control(u2_dev, RT_DEVICE_CTRL_CONFIG, (void *)&u2_configs);

    rt_device_set_rx_indicate(u2_dev, rx_callback);

    rt_sem_init(&sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);

    u2_th=rt_thread_create("u2_recv", u2_rcv_entry, NULL, 1024, 10, 10);
    rt_thread_startup(u2_th);

    return RT_EOK;

}



